﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;


namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class UWarrantyCardAgentList : PageBase
    {

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UWarrantyCardAgentList";
            }
        }
        #region 属性 (查询条件)


        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");

            }
        }
        protected string CustomerName
        {
            get
            {
                return GetRequestValue<string>("CustomerName", "");

            }
        }
        protected string CustomerIDCard
        {
            get
            {
                return GetRequestValue<string>("CustomerIDCard", "");

            }
        }

        protected string AgentName
        {
            get
            {
                return HasPower(Permission.经销商平台_经销商) ? OUserController.MutlCmpStr(base.User.UserId) : "";

            }
        }
        protected string searchAgentName
        {
            get
            {
                return GetRequestValue<string>("searchAgentName", "");

            }
        }
        protected string Birthday
        {
            get
            {
                return GetRequestValue<string>("Birthday", "");

            }
        }
        protected string AgentAddress
        {
            get
            {
                return GetRequestValue<string>("AgentAddress", "");

            }
        }
        protected bool ShowTimeInput
        {
            get
            {
                return GetRequestValue<bool>("ShowTimeInput", false);
            }
        }
        protected string IntoDateFrom
        {
            get
            {
                return ShowTimeInput ? DateTime.Now.AddMonths(-1).ToString("yyyy-MM-01") : GetRequestValue<string>("IntoDateFrom", "");

            }
        }
        protected string IntoDateTo
        {
            get
            {
                string _temp = ShowTimeInput ? DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") : GetRequestValue<string>("IntoDateTo", "");

                return IntoDateFrom.IsEmptyOrNull() ? "" : _temp.IsEmptyOrNull() ? Convert.ToDateTime(IntoDateFrom).AddDays(1).ToString("yyyy-MM-dd") : _temp;

                //  return _temp.IsEmptyOrNull() ? CreateDateFrom.IsEmptyOrNull() ? "" : Convert.ToDateTime(CreateDateFrom).AddDays(1).ToString("yyyy-MM-dd") :  CreateDateFrom.IsEmptyOrNull()?"":_temp;
            }
        }

        protected string MachineType
        {
            get
            {
                return GetRequestValue<string>("MachineType", "");
            }
        }

        protected override string orderField
        {
            get
            {
                return GetRequestValue<string>("orderfield", "IntoDate");
            }
        }

        protected override string orderDirection
        {
            get
            {
                return GetRequestValue<string>("orderDirection", "DESC");
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
        }
        protected string GetSQLWhere()
        {

            string wheresql = "";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SerialNumber))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SerialNumber);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(CustomerName))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("CustomerName", QueryOperatorsEnum.包含, CustomerName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(CustomerIDCard))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("CustomerIDCard", QueryOperatorsEnum.包含, CustomerIDCard);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(AgentName))
            {
                sqlcommonFiled = string.Format("AgentName in {0}",AgentName);// QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, AgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(searchAgentName))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, searchAgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(Birthday))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("Birthday", QueryOperatorsEnum.等于, Birthday);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(AgentAddress))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("AgentAddress", QueryOperatorsEnum.包含, AgentAddress);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }



            if (IntoDateFrom != "")
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IntoDate", QueryOperatorsEnum.范围, IntoDateFrom + "|" + IntoDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(MachineType))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("MachineType", QueryOperatorsEnum.包含, MachineType);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }

            return wheresql;
        }
        protected void DataBind()
        {
            //  string wheresql = QueryOperatorsEnumHelper.ConvertCommonOperator(CommonFiled, QueryOperatorsEnum.包含, CommonValue);

            this.rpMenu.DataSource = UWarrantyCardController.GetPagination(GetSQLWhere(), pageNum, numPerPage, SQLSort, ref TotalCount)[0];
            this.rpMenu.DataBind();
        }
    }
}